package io.aircraft.modules.wms.controller;

import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.exception.ErrorCode;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.modules.wms.dto.TaskAffirmDTO;
import io.aircraft.modules.wms.dto.TaskDTO;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.wms.service.StoredProcService;
import io.aircraft.modules.wms.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 业务信息
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-13
 */
@RestController
@RequestMapping("wms/task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @GetMapping("page")
    @RequiresPermissions("wms:task:page")
    public Result<PageData<TaskDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<TaskDTO> page = taskService.page(params);

        return new Result<PageData<TaskDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:task:info")
    public Result<TaskDTO> get(@PathVariable("id") Long id) {
        TaskDTO data = taskService.get(id);

        return new Result<TaskDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:task:save")
    public Result save(@RequestBody TaskDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        taskService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:task:update")
    public Result update(@RequestBody TaskDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        taskService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:task:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        taskService.delete(ids);

        return new Result();
    }

    @PostMapping("affirm")
    @LogOperation("出库确认")
    public Result affirm(@RequestBody TaskAffirmDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto);
        ProcResult procResult = taskService.affirm(dto);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }
}